﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Sina.Altman.Weibo.Tools;
using System.IO;
using Sina.Altman.Weibo.SDK.Objects;

namespace Sina.Altman.Weibo.SDK
{
    public class UserRequest
    {
        static string token = Enviroment.Token;
        /// <summary>
        /// 获取公共信息流，返回值可能为null注意检查
        /// 获取操作为异步操作
        /// </summary>
        /// <param name="parameters">可加参数</param>
        /// <returns></returns>
        public static void GetRequest(UserType type, UserRequestData data, Action<object> callback)
        {
            var request = (HttpWebRequest)WebRequest.Create(new Uri(GetRequestUri(type, data), UriKind.Absolute));
            request.Method = "GET";
            var inner = request.BeginGetResponse(ResponseCallback, new UserUserState() { Callback = callback, Request = request, RequestData = data,Type=type });

        }

        /// <summary>
        /// 回调 
        /// </summary>
        /// <param name="result"></param>
        private static void ResponseCallback(IAsyncResult result)
        {
            var userState = result.AsyncState as UserUserState;

            try
            {
                var request = userState.Request as HttpWebRequest;
                var respone = (HttpWebResponse)request.EndGetResponse(result);

                var stream = respone.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string jsonRes = reader.ReadToEnd();
                    if (!string.IsNullOrEmpty(jsonRes))
                    {
                        if (userState.Type == UserType.Counts)
                        {
                            jsonRes = jsonRes.Substring(1, jsonRes.Length - 2);
                            Count uid = new Count(jsonRes);
                            if (null != userState.Callback)
                            {
                                userState.Callback(uid);
                            }
                        }
                        else
                        {
                            User user = new User(jsonRes);
                            if (null != userState.Callback)
                            {
                                userState.Callback(user);
                            }
                        }
                        
                    }
                }
            }
            catch (Exception e)
            {
                if (null != userState.Callback)
                    userState.Callback(e);
            }
        }

        /// <summary>
        /// 获取请求地址
        /// </summary>
        /// <param name="queryType"></param>
        /// <returns></returns>
        private static string GetRequestUri(UserType queryType, UserRequestData data)
        {
            if (string.IsNullOrEmpty(token))
                token = ISHelper.LoadSetting<string>(Constants.User_Access_Token_SR, string.Empty);
            string fullUri = string.Empty;

            switch (queryType)
            {
                case UserType.Counts:
                    fullUri = Constants.USER_COUNTS + "?access_token=" + token + "&uids=" + data.UserID;
                    break;
                case UserType.UserShow:
                    fullUri = Constants.USER_SHOW + "?access_token=" + token + "&uid=" + data.UserID;
                    break;
                default:
                    break;
            }

            return fullUri;
        }
    }

    public class UserUserState:UserState
    {
        public UserRequestData RequestData { get; set; }

        public UserType Type { get; set; }
    }

    public enum UserType
    {
        Counts,
        UserShow,
    }

    public class UserRequestData:RequestData
    {

    }
}
